﻿using System;
using System.Text;
using System.Web;
using System.Web.Mvc;
using SimpleMvcCaptcha.Utils;

namespace SimpleMvcCaptcha.Helper
{
    /// <summary>
    /// Captcha expression operation type
    /// </summary>
    internal enum Operation
    {
        Plus,
        Minus
    }

    public static class CaptchaExtensions
    {
        /// <summary>
        /// Extension method for captcha generation
        /// </summary>
        /// <param name="helper">Standart HtmlHelper</param>
        /// <returns>String with html tags for captcha</returns>
        public static MvcHtmlString SimpleMvcCaptcha(this HtmlHelper helper)
        {
            return SimpleMvcCaptcha(helper, new CaptchaParameters());
        }

        /// <summary>
        /// Extension method for captcha generation
        /// </summary>
        /// <param name="helper">Standart HtmlHelper</param>
        /// <param name="parameters">Parameters object</param>
        /// <returns>String with html tags for captcha</returns>
        public static MvcHtmlString SimpleMvcCaptcha(this HtmlHelper helper, CaptchaParameters parameters)
        {
            var random = new Random();

            // Generation random values and ariphmetic operation
            var firstValue = random.Next(parameters.MaxNumber);
            var secondValue = random.Next(parameters.MaxNumber);
            var operation = random.Next(1000) > 500 ? Operation.Plus : Operation.Minus;

            // Calculating amount and deciding format of operation text (sign or words)
            int result;
            string operationText;

            if(operation == Operation.Plus)
            {
                result = firstValue + secondValue;
                operationText = random.Next(1000) > 500 ? parameters.PlusOperationText : "+";
            }
            else
            {
                result = firstValue - secondValue;
                operationText = random.Next(1000) > 500 ? parameters.MinusOperationText : "-";
            }

            var expression = string.Format("{0} {1} {2}", firstValue, operationText, secondValue);

            var captcha = new Captcha();
            captcha.Expresion = expression;
            captcha.Result = result.ToString();

            // Persisting result in serssion by guid
            var captchaId = Guid.NewGuid().ToString();
            HttpContext.Current.Session[captchaId] = captcha;
            
            // Making html string
            var urlHelper = ((Controller)helper.ViewContext.Controller).Url;
            var html = new StringBuilder();

            var div = new TagBuilder("div");
            div.AddCssClass("smc-captcha");

            html.AppendFormat("<img src='{0}' alt='captcha' class='smc-img-captcha' /><br/>",
                              urlHelper.Action(parameters.CaptchaAction, parameters.CaptchaController, new { id = captchaId }));
            html.AppendFormat("<input type='hidden' name='captchaId' value='{0}' />", captchaId /*CaptchaUtils.Encrypt(result.ToString(), parameters.SecretKey)*/);
            html.Append("<input type='text' name='captchaAnswer' class='smc-input-result' />");

            div.InnerHtml = html.ToString();

            return MvcHtmlString.Create(div.ToString());
        }
    }
}
